package com.contest.util;

import com.cloopen.rest.sdk.CCPRestSmsSDK;

import java.util.HashMap;
import java.util.Random;

public class SmsUtil {
    private static CCPRestSmsSDK restAPI = new CCPRestSmsSDK();
    static {
        restAPI.init("app.cloopen.com", "8883");
        restAPI.setAccount("8a216da86077dcd001609c27de6115b5", "42784b2bc35f4654baf5c1ea312aca4a");
        restAPI.setAppId("8a216da86077dcd001609c27debd15bc");
    }

    private static final String prefix="";

    /**
     * 发送手机验证码
     * @param phone
     * @return
     */
    public static String sendVerifyCode(String phone) {
        String[] codeArr ={"0","1","2","3","4","5","6","7","8","9"};
        String code = "";
        for (int i=0;i<4;i++){
            Random random = new Random();
            code += codeArr[random.nextInt(codeArr.length)];
        }
        HashMap<String, Object> result = restAPI.sendTemplateSMS(phone,"227067" ,new String[]{code,"3"});
        if("000000".equals(result.get("statusCode"))){
            return code;
        }else{
            return null;
        }
    }

    public static void main(String[] args) {
        String s = sendVerifyCode("18649823756");
        System.out.println(s);
    }
}
